package com.study.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.study.block_handler.StudyBlocHandler;
import com.study.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 基于 Sentinel 控制台配置流控规则
 * sentinel 是懒加载的、先去访问一下、就可以在sentinel dashboard看到了
 */
@Slf4j
@RestController
@RequestMapping("/dashboard")
public class RateLimitController {

    /**
     * 在dashboard 中 "流控规则"中按照资源名称新增流控规则
     * @return
     */
    @GetMapping("/by-resource")
    @SentinelResource(
            value = "byResource",
            blockHandler = "studyHandlerBlockException",
            blockHandlerClass = StudyBlocHandler.class
    )
    public CommonResponse<String> byResource(){

       log.info("coming in rate limit controller by resource");

       return new CommonResponse<>(0,"","byResource");

    }

    /**
     * 在 "簇点链路" 中给 url 添加流控规则
     * @return
     */
    @GetMapping("/by-url")
    @SentinelResource(
            value = "byUrl"
    )
    public CommonResponse<String> byUrl(){

        log.info("coming in rate limit controller by url");

        return new CommonResponse<>(0,"","byUrl");
    }

}
